package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyChengYiTongV2;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeChengYiTongV2;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 安心分期-V2&橙意通82S
 * @ Author 江小游
 * @ Date 2025/4/28 09:37
 */
@ApiPlan(productId = "172", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatChengYiTongPhoneMd5ConvertV2 implements ApiPlatformPhoneMd5ConvertHandler {
    private static final String KEY = "QlB1x6a7PLA3gd8D";
    private static final String ORG_ID = "302306472";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        // 实体类
        ApiPlatDataExchangeChengYiTongV2 data = new ApiPlatDataExchangeChengYiTongV2();
        //拓展
        ApiFieldConverter apiFieldConverter=ApiFieldConverter.create(apiDataExchangeParameter);
        // 获取加密的key
        data.setAppKey(KEY);
        // 获得手机号,小写
        data.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5().toLowerCase());
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  性别 1男 2女 0 未知
        Integer sex = ChooseEq.create()
                .when(apiDataExchangeParameter.getSex(), "0", 1)
                .when(apiDataExchangeParameter.getSex(), "1", 2)
                .when(apiDataExchangeParameter.getSex(), "2", 0)
                .end(0);
        data.setGender(sex);
        //  城市名称
        data.setCity(apiDataExchangeParameter.getCity());
        // 城市编码
        data.setCityCode(apiDataExchangeParameter.getAreaCode());
        //  职业 1:上班族 2:自由职业 3:企业主 4:公务员/国企
        Integer career = ChooseEq.create()
                .when(apiDataExchangeParameter.getCareer(), "1", 1)
                .when(apiDataExchangeParameter.getCareer(), "2", 2)
                .when(apiDataExchangeParameter.getCareer(), "3", 3)
                .when(apiDataExchangeParameter.getCareer(), "4", 4)
                .when(apiDataExchangeParameter.getCareer(), "5", 2)
                .end(1);
        data.setProfession(career);
        //  贷款额度 1:30000-50000 2:50000-10000 3:100000-200000
        Integer money = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanAmount(), "1", 1)
                .when(apiDataExchangeParameter.getLoanAmount(), "2", 2)
                .when(apiDataExchangeParameter.getLoanAmount(), "3", 3)
                .when(apiDataExchangeParameter.getLoanAmount(), "4", 3)
                .end(1);
        data.setLoanAmount(money);
        //  芝麻分 1:700以上 2:650-700 3:600-650 4:无
        Integer zhima = ChooseEq.create()
                .when(apiDataExchangeParameter.getSesameCredit(), "1", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "2", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "3", 2)
                .when(apiDataExchangeParameter.getSesameCredit(), "4", 1)
                .end(4);
        data.setZhima(zhima);
        //  社保 1:6个月以下 2:6-12个月 3:12个月以上 4:无
        Integer socialSecurity = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 4)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 1)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 1)
                .end(4);
        data.setSocialSecurity(socialSecurity);
        //  公积金 1:6个月以下 2:6-12个月 3:12个月以上 4:无
        Integer providentFund = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 4)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 1)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 1)
                .end(4);
        data.setProvidentFund(providentFund);
        //  房产 1:有 2:无
        Integer house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 1)
                .end(2);
        data.setHouse(house);
        //  车产 1:有 2:无
        Integer vehicle = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 1)
                .end(2);
        data.setVehicle(vehicle);
        // 上游渠道id
        data.setReferrerChannel("安心分期");
        data.setPhoneEight(apiDataExchangeParameter.getPhone().substring(0, 8));
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("orgId", ORG_ID);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }



    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyChengYiTongV2 data = new ApiPlatApplyChengYiTongV2();
        ApiFieldConverter apiFieldConverter=ApiFieldConverter.create(apiApplyParameter);
        // 姓名
        data.setName(apiApplyParameter.getUserName());
        // 加密的key
        data.setAppKey(KEY);
        // 撞库返回的id
        data.setOrderId(apiApplyParameter.dataExchangeApplyId());
        // 手机号
        data.setPhone(apiApplyParameter.getPhone());
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  性别 1男 2女 0 未知
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", 2)
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "2", 0)
                .end(0);
        data.setGender(sex);
        // 城市编号
        data.setCityCode(apiApplyParameter.getAreaCode());
        //  城市名称
        data.setCity(apiApplyParameter.getCity());
        //  职业 1:上班族 2:自由职业 3:企业主 4:公务员/国企
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 2)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 4)
                .when(apiApplyParameter.getCareer(), "5", 2)
                .end(1);
        data.setProfession(career);
        //  贷款额度 1:30000-50000 2:50000-10000 3:100000-200000
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 1)
                .when(apiApplyParameter.getLoanAmount(), "2", 2)
                .when(apiApplyParameter.getLoanAmount(), "3", 3)
                .when(apiApplyParameter.getLoanAmount(), "4", 3)
                .end(1);
        data.setLoanAmount(money);
        //  芝麻分 1:700以上 2:650-700 3:600-650 4:无
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 3)
                .when(apiApplyParameter.getSesameCredit(), "2", 3)
                .when(apiApplyParameter.getSesameCredit(), "3", 2)
                .when(apiApplyParameter.getSesameCredit(), "4", 1)
                .end(4);
        data.setZhima(zhima);
        //  社保 1:6个月以下 2:6-12个月 3:12个月以上 4:无
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 4)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 2)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(4);
        data.setSocialSecurity(socialSecurity);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 4)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 2)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(4);
        data.setProvidentFund(providentFund);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(2);
        data.setHouse(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 2)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(2);
        data.setVehicle(vehicle);
        // 用户授权url
        data.setAgreeProtocol(H5DomainUrlUtils.createUserProtocolUrl(apiApplyParameter.getProductPlanId(), apiApplyParameter.getPhoneMd5()));
        data.setReferrerChannel("安心分期");
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("orgId", ORG_ID);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if(!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");

        BigDecimal price = resData.getBigDecimal("price");
        String orderId = resData.getString("orderId");
        String url = resData.getString("redirectUrl");

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();


        result.setPrice(price);
        result.setLogoPath("https://static.fzysmkj.com/image_path/20250304102249A002_69d6ba6d2a32a8426d00006b5bae5da740a7bd9f.png");
        result.setApplyId(orderId);
        result.setCallUrl(url);
        result.setDisplayName("国润建融信息科技（上海）有限公司");
        result.setProductName("橙易通");
        return result;
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        JSONObject jsonObject = JsonUtils.get(requestVO.getBody());
        String orgId = jsonObject.getString("orderId");


        RespPlatformNoticeApply applyNotice = new RespPlatformNoticeApply();
        applyNotice.setApplyId(orgId);
        return applyNotice;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        boolean b = apiBizStatus.hasBizSuc();

        if (b) {
            return MapUtils.newHashMap(newHashMap -> {
                newHashMap.put("code", 0);
                newHashMap.put("msg", "操作成功");
            });
        }
        return MapUtils.newHashMap(newHashMap -> {
            newHashMap.put("code", 500);
            newHashMap.put("msg", "操作失败");
        });
    }
}
